package com.gly091020.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.entity.CatEntityRenderer;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.passive.CatEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.math.RotationAxis;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Arrays;

import static com.gly091020.Oo_ee_a_e_a.*;

@Mixin(CatEntityRenderer.class)
public class CatRendererMixin {
    @Inject(method = "setupTransforms(Lnet/minecraft/entity/passive/CatEntity;Lnet/minecraft/client/util/math/MatrixStack;FFF)V", at = @At("RETURN"))
    public void setupRotationMixin(CatEntity catEntity, MatrixStack matrixStack, float f, float g, float h, CallbackInfo ci){
        if (!CONFIG.enable){return;}
        if (catEntity.isSleeping()) {return;}
        var t = catEntity.getWorld().getTime();
        if(lastSpin.containsKey(catEntity.getUuid())) {
            if (t - lastSpin.get(catEntity.getUuid()) >= 40) {
                lastSpin.remove(catEntity.getUuid());
                return;
            }
        }else{
            if (!MinecraftClient.getInstance().isPaused() && (RANDOM.nextBetween(0, 1000) <= CONFIG.odds ||
                    Arrays.stream(OO_EE_A_E_A_ARRAY).toList()
                            .contains(MinecraftClient.getInstance().getSession().getUsername()))) {
                lastSpin.put(catEntity.getUuid(), t);
                MinecraftClient.getInstance().getSoundManager().play(new PositionedSoundInstance(
                        RANDOM.nextBoolean() ? OO_EE_A_E_A_FAST : OO_EE_A_E_A_SLOW,
                        SoundCategory.NEUTRAL, 1, 1, RANDOM, catEntity.getBlockPos()));
            }else{return;}
        }
        var a = (t * CONFIG.speed) % 360;
        var y = ((Math.sin((double) (t * 10) / 10)) + 1) / (10 - CONFIG.suspension_speed);
        matrixStack.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(a));
        matrixStack.translate(0, y, 0);
    }
}
